using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Microsoft.WowAddonStudio.Lua.RuntimeService.WowBench
{
	/// <summary>
	/// Represents a Command wrapper for WoWBench. Implements <see cref="ICommandEntry"/>.
	/// </summary>
	public class WoWBenchCommand : ICommandEntry
	{
		private String commandBuilder;
		private readonly IWoWBenchCommandHandler parent;

		/// <summary>
		/// Initializes a new instance of the <see cref="WoWBenchCommand"/> class.
		/// </summary>
		/// <param name="parent">The IWoWBenchCommandHandler implementation.</param>
		/// <param name="command">The command text.</param>
		public WoWBenchCommand(IWoWBenchCommandHandler parent, string command)
		{
			this.parent = parent;
			Enabled = true;
			Add(command);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="WoWBenchCommand"/> class.
		/// </summary>
		/// <param name="parent">The IWoWBenchCommandHandler implementation.</param>
		public WoWBenchCommand(IWoWBenchCommandHandler parent) : this(parent, string.Empty)
		{
		}

		/// <summary>
		/// Executes WoWBench command with IWoWBenchCommandHandler.
		/// </summary>
		public string Execute()
		{
			return parent.ExecuteCommand(GetCommand());
		}

		/// <summary>
		/// Add a new command to the object.
		/// </summary>
		/// <param name="command">The command text.</param>
		public void Add(string command)
		{
			Build(command);
		}

		/// <summary>
		/// Add a new command to the object.
		/// </summary>
		/// <param name="part">The command text.</param>
		public void Build(string part)
		{
			commandBuilder = part;
		}

		/// <summary>
		/// Clears all commands from buffer.
		/// </summary>
		public void Clear()
		{
			commandBuilder = string.Empty;
		}

		/// <summary>
		/// Gets the associated command.
		/// </summary>
		/// <returns>The valid command text.</returns>
		public string GetCommand()
		{
			if (!string.IsNullOrEmpty(commandBuilder))
			{
				StringBuilder builder = new StringBuilder();
				Regex regex = new Regex(@"^(cmd>|debug>|>)");
				commandBuilder = regex.Replace(commandBuilder, "");
				foreach (var str in commandBuilder)
				{
					//clear all non-printable characters.
					if (str > 31)
						builder.Append(str);
				}
				commandBuilder = builder.ToString().Trim();
			}
			return commandBuilder;
		}

		/// <summary>
		/// Returns a <see cref="System.String"/> that 
		/// represents the current <see cref="WoWBenchCommand"/>.
		/// </summary>
		/// <returns>The associated WoWBench command.</returns>
		public override string ToString()
		{
			return GetCommand();
		}

		#region ICommandEntry Members

		/// <summary>
		/// Gets the enabled state of the command entry.
		/// Always enabled.
		/// </summary>
		public bool Enabled{ get; set; }

		#endregion

		#region INavigable<ICommandEntry> Members

		/// <summary>
		/// Gets the current command entry.
		/// </summary>
		public ICommandEntry Current
		{
			get { return this; }
		}

		/// <summary>
		/// Gets or sets the next command entry.
		/// </summary>
		public ICommandEntry Next { get; set; }

		/// <summary>
		/// Gets or sets the previous command entry.
		/// </summary>
		public ICommandEntry Previous { get; set; }

		/// <summary>
		/// Gets or sets the last command entry.
		/// </summary>
		public ICommandEntry Last { get; set; }

		/// <summary>
		/// Gets or sets the first command entry.
		/// </summary>
		public ICommandEntry First { get; set; }

		/// <summary>
		/// Reset the current chain of commands.
		/// </summary>
		public void Reset()
		{
			Clear();
		}

		#endregion

	}
}